package com.example.administrator.mymusic.Class;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.mymusic.R;

import java.util.List;

/**
 * Created by DAB on 2016/4/24 22:55.
 */
public class MyLrcAdapter extends BaseAdapter {
    private List<LrcClass> lrcClassList;
    private LayoutInflater inflater;
    private int firstVisibleItem = 0;
    private int visibleItemCount = 0;
    private int totalItemCount = 0;

    public MyLrcAdapter(Context context, List<LrcClass> lrcClassList) {
        this.lrcClassList = lrcClassList;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return lrcClassList.size();
    }

    @Override
    public LrcClass getItem(int position) {
        return lrcClassList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_lrc, null);
            viewHolder = new ViewHolder();
            viewHolder.tvLrc = (TextView) convertView.findViewById(R.id.tv_item_lrc);
            viewHolder.llItemShang = (LinearLayout) convertView.findViewById(R.id.ll_item_lrc_shang);
            viewHolder.llItemXia = (LinearLayout) convertView.findViewById(R.id.ll_item_lrc_xia);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Log.d("55555", "getView: " + position);
        viewHolder.llItemShang.setVisibility(View.GONE);
        viewHolder.llItemXia.setVisibility(View.GONE);
        if (position == 0) {
            viewHolder.llItemShang.setVisibility(View.VISIBLE);
        }
        if (position == lrcClassList.size() - 1) {
            viewHolder.llItemXia.setVisibility(View.VISIBLE);
        }
//        Log.d("77777", "getView: " + (position + 7) + "******" + totalItemCount);

        if (firstVisibleItem + (visibleItemCount - 7) == position) {
            Log.d("77777", "getView: " + firstVisibleItem + "------" + position);
            viewHolder.tvLrc.setTextSize(22);
            viewHolder.tvLrc.setTextColor(Color.YELLOW);
            viewHolder.tvLrc.setText(lrcClassList.get(position).getLrcStr());
        } else {
            if (totalItemCount > position + 7) {
                viewHolder.tvLrc.setText(lrcClassList.get(position).getLrcStr());
                viewHolder.tvLrc.setTextSize(16);
                viewHolder.tvLrc.setTextColor(Color.WHITE);
            }
        }

        return convertView;
    }

    class ViewHolder {
        private TextView tvLrc;
        private LinearLayout llItemShang;
        private LinearLayout llItemXia;

    }


    public void setVisibleItem(int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        this.firstVisibleItem = firstVisibleItem;
        this.visibleItemCount = visibleItemCount;
        this.totalItemCount = totalItemCount;
        Log.d("333333", "setVisibleItem: " + firstVisibleItem);
        Log.d("555555", "setVisibleItem i: " + visibleItemCount);
    }
}
